#ifndef __MAIN_H
#define __MAIN_H

// Internal MCU peripherals
#include "gpio.h"
#include "spi.h"

// External peripherals
#include "ssd1306.h"

// Other libraries
#include "delay.h"

// Graphical resources
#include "font5x7.h"
#include "font7x10.h"


// Some common variables
uint32_t i,j,k;


// Picture from here http://pixeljoint.com/pixelart/67237.htm
// Size: 128 x 64
static const uint8_t bmp_dangerous_tunnels[] = {
		0x74,0x68,0x72,0x68,0x34,0x1F,0x00,0x1F,0x34,0x68,0x72,0x68,0x74,0x68,
		0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,
		0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,0x34,0x1F,0x00,0x1F,0x34,0x68,
		0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,
		0x74,0x69,0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,0x34,0x1F,
		0x00,0x1F,0x34,0x68,0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,0x72,0x68,
		0x74,0x68,0x72,0x68,0x74,0x69,0x72,0x68,0x74,0x68,0x72,0x68,0x74,0x68,
		0x72,0x68,0x34,0x1F,0x00,0x55,0x2A,0x00,0x2A,0x00,0x14,0xFF,0x87,0x38,
		0x38,0x38,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x38,0x38,
		0x38,0x87,0x04,0x01,0x04,0x11,0x45,0x17,0x40,0x10,0x52,0x75,0x02,0x05,
		0x22,0x57,0x20,0x55,0x27,0x77,0x07,0x57,0x77,0x77,0x70,0x00,0x1D,0x3D,
		0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x1D,0x00,0x77,
		0x77,0x57,0x07,0x77,0x27,0x55,0x20,0x57,0x22,0x04,0x01,0x71,0x52,0x04,
		0x19,0xE3,0x1F,0x07,0x02,0x04,0x59,0x07,0x03,0x01,0x04,0x01,0x05,0x01,
		0x04,0x01,0x00,0x01,0x04,0x01,0x05,0x01,0x04,0x01,0x00,0x01,0x04,0x01,
		0x05,0x01,0x04,0x01,0x00,0x01,0x04,0x01,0x05,0x01,0x04,0x01,0xC0,0x80,
		0x00,0x00,0x41,0x11,0x40,0x41,0x3E,0x55,0x2A,0x00,0x2A,0x00,0x14,0xFF,
		0x87,0x38,0x38,0x38,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,
		0x38,0x38,0x38,0x87,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
		0x04,0x11,0x05,0x13,0x40,0x15,0x52,0x31,0x03,0x55,0x33,0x55,0x30,0x55,
		0x33,0x57,0x03,0x57,0x33,0x55,0x30,0x55,0x33,0x57,0x03,0x77,0x33,0x55,
		0x20,0x57,0x23,0x55,0x02,0x35,0x52,0x15,0x40,0x13,0x05,0x11,0x04,0x01,
		0x00,0x01,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0x80,0x80,0x80,
		0x80,0x00,0x00,0x00,0x02,0x8E,0xDC,0xDC,0xD8,0x60,0x78,0x3C,0xBC,0x98,
		0x5B,0x4B,0x2B,0x2C,0x0C,0x0D,0x8D,0x4D,0x38,0x01,0x3A,0x11,0x2A,0x17,
		0x15,0xFF,0x87,0x39,0x38,0x39,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,
		0x28,0x28,0x38,0x38,0x38,0x87,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x10,0xFC,0x9E,0x9C,0x9C,0x1D,0x1C,0x9D,0x9C,0x9D,0x9C,0xFE,
		0x16,0x24,0x00,0x01,0x05,0x01,0x04,0x01,0x00,0x01,0x04,0x01,0x05,0x01,
		0x04,0x01,0x00,0x03,0x05,0x01,0x04,0x01,0x00,0x01,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x70,0x10,0x10,0x00,0x17,0x01,0x11,0x00,0x71,
		0x10,0x11,0x00,0x17,0x01,0x11,0x00,0x01,0x00,0x00,0x00,0x00,0x01,0x81,
		0xC3,0xE7,0xEF,0xEF,0xF6,0xF8,0x7F,0x3F,0x1F,0x0D,0x04,0x82,0x43,0x21,
		0x90,0xD0,0x10,0x10,0x08,0x04,0x82,0x91,0xC0,0x00,0x3E,0x55,0x2A,0x00,
		0x2A,0x00,0x14,0x7F,0x87,0x38,0x38,0x38,0x28,0x28,0x28,0x28,0x28,0x28,
		0x28,0x28,0x28,0x28,0x38,0x38,0x38,0x87,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x80,0x40,0xA0,0xA0,0x23,0x27,0x67,0x60,0x4B,0x1F,0x3F,0xBF,0xBC,
		0xBC,0xB7,0x82,0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
		0x80,0x80,0x80,0x80,0x80,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,
		0xDE,0xDF,0x0B,0x2B,0x0B,0x89,0xE9,0xE8,0xE8,0xC8,0xC4,0xD4,0xF2,0xB9,
		0x5C,0xFF,0x83,0x10,0x4C,0xCE,0x67,0xE7,0x75,0xB4,0xB4,0xB4,0xF4,0xFE,
		0xFE,0xFE,0xF3,0xF1,0xF5,0xF4,0xB7,0x20,0x28,0x08,0x28,0x28,0x28,0x28,
		0x28,0x28,0x28,0x28,0x28,0x28,0x38,0x38,0x38,0x87,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x9C,0x3E,0x3E,0x9E,0x9E,0x0E,0xC0,0x8F,0x47,0x8F,0x5E,
		0x9E,0x1E,0x0D,0x4B,0x56,0xCE,0xCE,0x4F,0x40,0x0D,0x16,0x1A,0x1A,0x1A,
		0x1A,0x0A,0x06,0x0C,0x01,0x0D,0x0F,0x0D,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x06,0x37,0x17,0x07,0x37,0x17,0x0F,0x6F,0x1B,0x3C,0x77,0xF8,0xFF,
		0xEF,0x4F,0x5F,0xAE,0x24,0x18,0x00,0x02,0xFD,0xC2,0xF1,0xFC,0xFE,0xFF,
		0xFF,0xFF,0x1F,0x3F,0x5E,0xBF,0x7F,0xFE,0xF7,0xFF,0xFF,0xF3,0x32,0x20,
		0x08,0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x38,0x38,0x38,0x87,0x00,0x00,
		0x00,0x00,0x00,0xC0,0xE0,0xF3,0x70,0xF2,0x72,0x00,0x38,0x3D,0x58,0xA5,
		0x50,0xA1,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x60,0xA0,0xE0,0xC0,0x80,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x1F,0x12,0x14,0x14,0x1D,0x1C,0x1C,0x1D,0x1C,0x9E,0x4F,0x86,
		0x01,0x83,0x04,0x84,0x42,0x81,0x40,0x80,0x40,0xA0,0x50,0xA3,0x57,0x27,
		0x4F,0x0F,0x1F,0xFE,0xFE,0xFE,0xFC,0x78,0xF1,0xC4,0x81,0xB3,0xA7,0x0F,
		0x1F,0x3F,0x3A,0xF0,0x78,0xE0,0xE0,0xF0,0xC0,0xE8,0x60,0x30,0x00,0x87,
		0x00,0x60,0x70,0x58,0x5B,0x5B,0x4A,0x76,0x07,0x01,0x00,0x00,0x00,0x00,
		0x00,0x5A,0x69,0x5A,0x69,0x5A,0x70,0x60,0x60,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x46,0x44,0x66,0x7C,0x7F,0x61,
		0x47,0x48,0x50,0x60,0x60,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x02,0x05,0x02,
		0x01,0x02,0x01,0x00,0x01,0x02,0x01,0x0A,0x15,0x2A,0x15,0x22,0x01,0x00,
		0x40,0x61,0x70,0x71,0x66,0x70,0x7F,0x3F,0x1E,0x0E,0x07,0x07,0x13,0x0B,
		0x01,0x01,0x00,0x00,0x02,0x03,0x03,0x03,0x03,0x01,0x01,0x00,0x00,0x00,
		0x09,0x13
};

// Tile bitmap
static const uint8_t bmp_tile[] = {
		0x38,0x44,0x82,0x29,0x11,0x29,0x82,0x44,0x38,0x44,0x82,0x11,0x29,0x11,
		0x82,0x44,0x38,0x44,0x82,0x11,0x29,0x11,0x82,0x44,0x38,0x44,0x82,0x29,
		0x11,0x29,0x82,0x44,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x00,
		0x00,0x01,0x01,0x01,0x00,0x00
};

// Sine wave look-up table
static uint8_t sine_LUT[] = {
		0x0c,0x0d,0x0e,0x0f,0x11,0x12,0x13,0x14,0x14,0x15,0x16,0x17,0x17,0x17,0x18,0x18,
		0x18,0x18,0x18,0x17,0x17,0x17,0x16,0x15,0x14,0x14,0x13,0x12,0x11,0x0f,0x0e,0x0d,
		0x0c,0x0b,0x0a,0x09,0x07,0x06,0x05,0x04,0x04,0x03,0x02,0x01,0x01,0x01,0x00,0x00,
		0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x03,0x04,0x04,0x05,0x06,0x07,0x09,0x0a,0x0b
};


#endif // __MAIN_H
